package com.xmg.day05;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Author: mazhongqing
 * @Date: 2020/10/23 10:05
 */
public class CompletableFutureDemo {
    public static void main(String[] args) throws IOException {
        long start,end;

        MyTask task1 = new MyTask("task1",3,true);
        MyTask task2 = new MyTask("task2",4,true);
        MyTask task3 = new MyTask("task3",1,false);

        CompletableFuture f1 = CompletableFuture.supplyAsync(()->task1.call())
                .thenAccept((result) -> callback(result));
        CompletableFuture f2 = CompletableFuture.supplyAsync(()->task2.call())
                .thenAccept((result) -> callback(result));
        CompletableFuture f3 = CompletableFuture.supplyAsync(()->task3.call())
                .thenAccept((result) -> callback(result));

        System.in.read();
    }

    private static void callback(Boolean result){
        if(result==false){
            System.out.println("出问题了！！！");
            System.exit(0);
        }
    }


    private static class MyTask{
        private String name;
        private Integer timeInSecond;
        private boolean status;

        public MyTask(String name,Integer timeInSecond,boolean status){
            this.name = name;
            this.timeInSecond = timeInSecond;
            this.status = status;
        }

        public boolean call(){
            try {
                TimeUnit.SECONDS.sleep(timeInSecond);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name + " task callback");
            return status;
        }
    }
}
